package com.android.pindaojia.fragment.member;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.pindaojia.model.member.SeaAmoyMain;
import com.android.pindaojia.R;
import com.android.pindaojia.fragment.group.Fragment_SAMain_TabList;
import com.android.pindaojia.fragment.group.Fragment_SeaAmoyTCList;
import com.android.pindaojia.model.member.SeaAmoyMainCallback;
import com.android.pindaojia.utils.ConstantsUrl;
import com.android.pindaojia.utils.HttpUtils;
import com.android.pindaojia.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.android.pindaojia.R.id.main_top_bg;

/**
 * Created by Administrator on 2017/7/6.
 */

public class Fragment_SeaAmoy  extends Fragment {
@BindView(R.id.no_data)
RelativeLayout noData;
@BindView(R.id.no_data_tv)
TextView noDataTv;
@BindView(R.id.progress)
RelativeLayout progress;
@BindView(R.id.click_resetnetwork)
LinearLayout clickResetnetwork;
@BindView(R.id.tab)
TabLayout tabLayout;
@BindView(R.id.pager)
ViewPager viewPager;
    @BindView(R.id.main_top_title)
    TextView mainTopTitle;
    @BindView(main_top_bg)
    Toolbar mainTopBg;

private static final String ARG_PARAM1 = "param1";
private LayoutInflater inflater;
private View view;
private FragmentActivity mcontext;
private boolean networkConnected;
private ArrayList<SeaAmoyMain.DataBean.Cat_list> cat_list = new ArrayList<>();
    private GroupFragmentPagerAdapter pagerAdapter;
private String country_id="";
private int page=1;
public  Fragment_SeaAmoy() {
        }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_seaamoymain, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
      mainTopTitle.setText("全球购");
      mainTopTitle.setTextColor(Color.parseColor("#ffffff"));
      mainTopBg.setBackgroundColor(Color.parseColor("#FF2741"));
      GetSAMain();
        return view;
        }
    private  void initUI()
        {
           pagerAdapter = new GroupFragmentPagerAdapter(getChildFragmentManager());
            viewPager.setAdapter(pagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }



    private class GroupFragmentPagerAdapter extends FragmentStatePagerAdapter {
    FragmentManager fm;
    public GroupFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this.fm=fm;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            return Fragment_SAMain_TabList.newInstance();

        } else {
            return Fragment_SeaAmoyTCList.newInstance(cat_list.get(position).getId());
           }

    }

    @Override
    public int getCount() {
        return cat_list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return cat_list.get(position).getCatname();
    }
}


    private void GetSAMain()
    {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.NATIONLISTDATA ;
        if (networkConnected) {

            HttpUtils.get(address,null, new SeaAmoyMainCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SeaAmoyMain response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {
                        cat_list.clear();

                        cat_list.add(new SeaAmoyMain.DataBean.Cat_list("0", "全部"));
                        if (response.getData() != null && response.getData().getCat_list().size() > 0) {
                            List<SeaAmoyMain.DataBean.Cat_list> data = response.getData().getCat_list();
                            cat_list.addAll(data);
                        }

                        initUI();
                    }else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }
                }
            });

        } else{

            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "连接连接出现异常");
        }
    }
}
